#include "RadianEditorController.h"
#include "RadianRenderWnd.h"

#include "OgreCamera.h"
#include "OgreRenderWindow.h"
#include "OgreSceneNode.h"

#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QFocusEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPixmap>
#include <QPen>
#include <QBrush>
#include <QRect>
//--------------------------------------------------------------------------
RadianEditorController::RadianEditorController(Ogre::Camera* camera, QWidget* parentWidget,RadianRenderWnd* parentRenderWindow)
	:mTargetCamera(camera)
	,mRadianBrowserMode(RBM_NORMAL)
	,mIsLock(false)
	,mRadianBrowserSenstive(RBS_NORMAL)
	,mLastLeftMousePos(0,0)
	,mLastRightMousePos(0,0)
	,mParentWidget(parentWidget)
	,mParentRenderWindow(parentRenderWindow)
	,mEditMode(Edit_None)
	,mSelectedNode(NULL)
{	
	mParentWidget->installEventFilter(this);
}
//--------------------------------------------------------------------------
RadianEditorController::~RadianEditorController()
{
	mTargetCamera = NULL;	
}
//--------------------------------------------------------------------------
void RadianEditorController::MoveRelative(float delta)
{
	if(mTargetCamera)
	{
		mTargetCamera->moveRelative(Math::Vector3(0,0,(int)mRadianBrowserSenstive * delta) * -0.01f);
	}
}
//--------------------------------------------------------------------------
void RadianEditorController::MoveDelta(const Math::Vector3& delta)
{
	if(mTargetCamera)
	{
		mTargetCamera->move(delta);
	}
}
//--------------------------------------------------------------------------
void RadianEditorController::Yaw(float rotate)
{
	if(mTargetCamera)
	{
		Math::Radian radianX = Math::Radian(-rotate / 500.0f);
		mTargetCamera->yaw(radianX);
	}
}
//--------------------------------------------------------------------------
void RadianEditorController::Pitch(float rotate)
{
	if(mTargetCamera)
	{
		Math::Radian radianY = Math::Radian(-rotate / 500.0f);
		mTargetCamera->pitch(radianY);
	}
}
//--------------------------------------------------------------------------
void RadianEditorController::NotifyCamera(Ogre::Camera* camera)
{
	mTargetCamera = camera;
}
//--------------------------------------------------------------------------
void RadianEditorController::LockSceneNode(const Ogre::String& sceneNodeName)
{
	mLockedNodeName = sceneNodeName;
}
//--------------------------------------------------------------------------
void RadianEditorController::FocusTo(Ogre::SceneNode* targeNode)
{

}
//--------------------------------------------------------------------------
void RadianEditorController::Update(float timeElapsed)
{
	return;
}
//--------------------------------------------------------------------------
void RadianEditorController::OnResize(QResizeEvent* evt)
{

}
//--------------------------------------------------------------------------
void RadianEditorController::OnKeyPress(QKeyEvent* evt)
{

}
//--------------------------------------------------------------------------
void RadianEditorController::OnKeyRelease(QKeyEvent* evt)
{

}
//--------------------------------------------------------------------------
void RadianEditorController::OnMouseMove(QMouseEvent* evt)
{
	
	if(evt->buttons() & Qt::LeftButton)
	{

	}

	else if(evt->buttons() & Qt::RightButton)
	{
		QPointF currentPos = evt->posF();
		float deltaX = currentPos.x() - mLastRightMousePos.x();
		float deltaY = currentPos.y() - mLastRightMousePos.y();

		Yaw(deltaX);
		Pitch(deltaY);

		mLastRightMousePos = evt->posF();
	}
}	
//--------------------------------------------------------------------------
void RadianEditorController::OnMousePress(QMouseEvent* evt)
{
	mParentWidget->setFocus();

	if(evt->button() == Qt::LeftButton)
	{
		mLastLeftMousePos = evt->posF();
	}

	else if(evt->button() == Qt::RightButton)
	{
		mLastRightMousePos = evt->posF();
	}
}
//--------------------------------------------------------------------------
void RadianEditorController::OnMouseRelease(QMouseEvent* evt)
{

}
//--------------------------------------------------------------------------
void RadianEditorController::OnMouseDoublePress(QMouseEvent* evt)
{
	
}
//--------------------------------------------------------------------------
void RadianEditorController::OnMouseWheel(QWheelEvent* evt)
{
	MoveRelative((float)evt->delta());
}
//--------------------------------------------------------------------------
void RadianEditorController::OnLostFocus(QFocusEvent* evt)
{
	mParentRenderWindow->GetRenderWindow()->setActive(true);
	mParentRenderWindow->GetRenderWindow()->update(true);
}
//--------------------------------------------------------------------------
void RadianEditorController::OnPaint(QPaintEvent* evt)
{
	// do nothing.
}
//--------------------------------------------------------------------------
bool RadianEditorController::eventFilter(QObject *target, QEvent *event)
{
	if(target == mParentWidget)
	{
		switch(event->type())
		{
		case QEvent::KeyPress:
			{
				// do key event.
				QKeyEvent* keyPressEvent = static_cast<QKeyEvent*>(event);
				OnKeyPress(keyPressEvent);
			}
			break;
		case QEvent::KeyRelease:
			{
				OnKeyRelease(static_cast<QKeyEvent*>(event));
			}
			break;
		case QEvent::MouseMove:
			{
				// do mouse event.
				OnMouseMove(static_cast<QMouseEvent*>(event));
			}
			break;
		case QEvent::MouseButtonDblClick:
			{
				OnMouseDoublePress(static_cast<QMouseEvent*>(event));
			}
			break;
		case QEvent::MouseButtonPress:
			{
				OnMousePress(static_cast<QMouseEvent*>(event));
			}
			break;
		case QEvent::MouseButtonRelease:
			{
				OnMouseRelease(static_cast<QMouseEvent*>(event));
			}
			break;
		case QEvent::Wheel:
			{
				OnMouseWheel(static_cast<QWheelEvent*>(event));
			}
			break;
		case QEvent::Resize:
			{

			}
			break;
		case QEvent::FocusOut:
			{
				OnLostFocus(static_cast<QFocusEvent*>(event));			
			}
			break;
		case QEvent::ShortcutOverride:
			{
				QKeyEvent* keyPressEvent = static_cast<QKeyEvent*>(event);
				if (keyPressEvent->key() == Qt::Key_Alt || keyPressEvent->key() == Qt::Key_Meta && keyPressEvent->modifiers() == Qt::AltModifier)
				{
					return false;
				}
			}
			break;
		case QEvent::Paint:
			{
				QPaintEvent* keyPressEvent = static_cast<QPaintEvent*>(event);
				OnPaint(keyPressEvent);
				
			}
			break;
		}
	}

	return QObject::eventFilter(target,event);
}
//--------------------------------------------------------------------------
Ogre::SceneNode* RadianEditorController::GetSelectedSceneNode()
{
	return mSelectedNode;
}
//--------------------------------------------------------------------------
const SceneNodeList& RadianEditorController::GetSelectedSceneNodes()
{
	return mSelectedNodes;
}
//--------------------------------------------------------------------------
RadianRenderWnd* RadianEditorController::GetParentRenderWnd()
{	
	return mParentRenderWindow;
}
//--------------------------------------------------------------------------
QWidget* RadianEditorController::GetParentWidget()
{	
	return mParentWidget;
}
//--------------------------------------------------------------------------